package cn.wit.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

/**
 * 统计java里面得关键字
 * 
 * @author 16604
 *
 */
public class DemoStatisticalJavaKeyword {

	// java里面得关键字
	private static final String CONTENT = "abstract	continue	for	new	switch"
			+ "assert***	default	goto*	package	synchronized" + "boolean	do	if	private	this"
			+ "break	double	implements	protected	throw" + "byte	else	import	public	throws"
			+ "case	enum****	instanceof	return	transient" + "catch	extends	int	short	try"
			+ "char	final	interface	static	void" + "class	finally	long	strictfp**	volatile"
			+ "const*	float	native	super	while";

	@Test
	public void count() throws IOException {

		// 把java字符串分解出来，放进一个字符串数组里面
		String[] s = CONTENT.split("[\t]");

		// 并且把每个关键字存放到set集合里面
		Set<String> set = new HashSet<>();
		for (int i = 0; i < s.length; i++) {
			set.add(s[i]);
		}

		// 读取文件内容
		String file = "D:\\暑期作业与笔记\\camp\\code\\day09\\src\\cn\\wit\\test\\DemoCheckException.java";
		List<String> list = Files.readAllLines(Paths.get(file));

		// 关键字与出现次数是映射关系，所以这里可以使用Map数组
		Map<String, Integer> map = new HashMap<>();

		// 解析单词
		for (String lines : list) {
		
			String[] word = lines.split("[ .?;()<>]");

			// 比较关键字
			for (String words : word) {
				if (set.contains(words)) {
					if (map.containsKey(words)) {
						Integer count = map.get(words);
						map.put(words, ++count);
					} else {
						map.put(words, 1);
					}
				}
			}
		}

		// 把map打印出来
		for (Entry<String, Integer> entry : map.entrySet()) {
			System.out.printf("%s \t %d \n", entry.getKey() , entry.getValue());
		}
	}
}
